using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace Evolution.VirtualFileSystem
{
    public class ResourceLibrary
    {
        #region Fields
        private string m_FileName;
        private ZipFile m_ZipFile;
        #endregion

        #region Properties
        public string FileName
        {
            get { return m_FileName; }
            set { m_FileName = value; }
        }

        #endregion

        #region Methods
        /// <summary>
        /// Constructor, will create the zip file if it does not exist
        /// </summary>
        /// <param name="ZipFile"></param>
        public ResourceLibrary(string ZipFile)
        {
            m_FileName = ZipFile;
            if (File.Exists(m_FileName))
                m_ZipFile = new ZipFile(m_FileName);
            else
                CreateZip();
        }

        /// <summary>
        ///  Create the zip file
        /// </summary>
        private void CreateZip()
        {
            FastZip fz = new FastZip();
            fz.CreateZip(m_FileName, "", false, "");
            fz = null;

            m_ZipFile = new ZipFile(m_FileName);
        }

        /// <summary>
        /// Recreates the zip file, and clears all content
        /// </summary>
        public void NewZipFile()
        {
            File.Delete(m_FileName);
            CreateZip();
        }

        /// <summary>
        ///  Add a file to the zip file
        /// </summary>
        /// <param name="FileName"></param>
        public void AddFile(string FileName)
        {
            ZipEntry mEntry = new ZipEntry(FileName);       
            m_ZipFile.BeginUpdate();           
            m_ZipFile.Add(FileName);
            m_ZipFile.CommitUpdate();
        }

        /// <summary>
        /// Gets a file from the Zip file
        /// </summary>
        /// <param name="FileName">File name inside the zip</param>
        /// <returns></returns>
        public Stream GetFile(string FileName)
        {
            foreach (ZipEntry entry in m_ZipFile)
            {
                string[] fileName = entry.Name.Replace("/", "\\").Split('.');
                if (fileName[0].ToLower().Contains(FileName.ToLower()))
                    return m_ZipFile.GetInputStream(entry);
            }

            return null;
        }

        /// <summary>
        /// Return a list of files
        /// </summary>
        /// <returns></returns>
        public string[] GetListOfFiles()
        {
            StringCollection scol = new StringCollection();

            foreach (ZipEntry entry in m_ZipFile)
            {
                scol.Add(entry.Name);
            }

            string[] sarr = new string[scol.Count];
            scol.CopyTo(sarr, 0);
            return sarr;
        }

        public void DeleteFile(string fileName)
        {
            ZipEntry mEntry = new ZipEntry(fileName);
            if (mEntry.Size != -1)
            {
                m_ZipFile.BeginUpdate();
                m_ZipFile.Delete(mEntry);
                m_ZipFile.CommitUpdate();
                m_ZipFile.Close();
            }
        }

        public void CloseFile()
        {
            m_ZipFile.Close();
        }

        #endregion

        public bool Contains(string assetName)
        {
            foreach (ZipEntry entry in m_ZipFile)
            {
                string[] fileName = entry.Name.Replace("/", "\\").Split('.');
                if( fileName[0].ToLower().Contains(assetName.ToLower()))
                    return true;
            }

            return false;
        }
    }
}
